﻿/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef REALMD_NET_AUTHSOCKETHANDLER_H_
#define REALMD_NET_AUTHSOCKETHANDLER_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include <ctime>
#include <string>
#include "Shared/Common.h"
#include "Shared/Crypto/BigNumber.h"
#include "Shared/Crypto/Sha1.h"
#include "Shared/Binary/ByteBuffer.h"
#include "Shared/Net/SocketEventHandler.h"

class AuthSocketHandler: public Shared::Net::SocketEventHandler
{
public:
    AuthSocketHandler(Shared::Net::Socket &peer);
    virtual ~AuthSocketHandler(void);

    std::uint32_t accountId(void) const { return _accountId; }

    void markClose(void);
    bool markedClose(void) const { return _closing; }
    void checkDeadLink(time_t maxAuthTime);
    bool tryClose(void);


    bool handleLogonChallenge(void);
    bool handleLogonProof(void);
    bool handleReconnectChallenge(void);
    bool handleReconnectProof(void);
    bool handleRealmList(void);

protected:
    virtual void onOpen(void);
    virtual void onRead(void);
    virtual void onWrite(void);
    virtual void onEof(void);
    virtual void onClose(void);
    virtual void onError(void);
    virtual void onTimeout(int flags);

private:
    void setVSFields(const std::string& rI);
    void sendProof(Shared::Crypto::Sha1Hash& sha);
    void loadRealmlist(Shared::Binary::ByteBuffer& pkt, std::uint32_t id);

    void setPacketProcess(bool processing) { _packetProcessing = processing; }


    ///////////////////////////////////////////////////////////////////////////
    bool _authed;
    time_t _connTime;
    volatile bool _closing;
    volatile bool _packetProcessing;

    ///////////////////////////////////////////////////////////////////////////
    std::uint32_t _accountId;               /* 登录帐号ID */
    std::string _loginName;                 /* 登录用户名 */
    std::string _safeLoginName;             /* 转码后的登录用户名，防止SQL注入 */
    std::uint16_t _build;                   /* 客户端版本号 */
    std::string _localizationName;          /* 地区名 */
    Shared::eAccountTypes _accountsecLv;    /* 帐号等级 */

    Shared::Crypto::BigNumber N, s, g, v;
    Shared::Crypto::BigNumber b, B;
    Shared::Crypto::BigNumber K;
    Shared::Crypto::BigNumber _reconnectProof;
};

#endif // REALMD_NET_AUTHSOCKETHANDLER_H_
